/**
 * Created by luochangming on 15/11/5.
 */
'use strict';

angular.module('myApp.nearLaidian', ['ngRoute'])

    .config(['$routeProvider', function($routeProvider) {
        $routeProvider.when('/nearLaidian', {
            templateUrl: 'nearLaidian/nearLaidian.html',
            controller: 'nearLaidianCtrl'
        })
            //.when('/nearLaidian/', {
            //    templateUrl: 'nearLaidian/nearLaidian.html',
            //    controller: 'nearLaidianCtrl'
            //});
    }])



    .controller('nearLaidianCtrl', ['$scope','$http','lHTTP',function($scope,$http,lHTTP) {
        var sortTime = 0;
        $scope.showPopTips = false;

        $scope.myMethod = function(){
            alert("aa");
        };




                    lHTTP.jsonp('/nearbyGetList?lat='+lat+'&lng='+lng)
                        .success(function(jsonData,status){
                            sortTime = jsonData.sortTime;
                            $scope.addressData = jsonData.data;
                        });




        $scope.lastPage = false;

        $scope.loadMore = function() {
            //if ($scope.currentPage < $scope.pages) {
            //    $scope.currentPage++;
                if ($scope.busy) {
                    return false;
                }

                if(!$scope.lastPage){
                    $scope.busy = true;
                    lHTTP.jsonp("/nearbyGetList?sortTime="+sortTime+"&lat="+lat+"&lng="+lng).success(function(jsonData) {
                        $scope.busy = false;
                        sortTime = jsonData.sortTime;

                        if(jsonData.data == undefined){
                            $scope.lastPage = true;
                        }

                        for (var i in jsonData.data) {
                            $scope.addressData.push(jsonData.data[i]);
                        }
                    });
                }
        };


    }])

.directive('whenScrolled', function() {
    return function(scope, elm, attr) {
        var raw = elm[0];
        elm.bind('scroll', function() {
            if (raw.scrollTop+raw.offsetHeight >= raw.scrollHeight) {
                scope.$apply(attr.whenScrolled);
            }
        });
    };
});